using UnityEngine;

public class RocketLeagueBallPhoton : PhotonTransformView
{
	public RocketLeagueBall rocketLeagueBall;

	private Vector3 ballPosition = Vector3.zero;

	private Vector3 ballVelocity = Vector3.zero;

	private Vector3 ballAngularVelocity = Vector3.zero;

	private float updateTime;

	private bool dataUpdated;

	private int ping;

	private void Start()
	{
		PhotonNetwork.sendRate = 30;
		PhotonNetwork.sendRateOnSerialize = 30;
	}

	public override void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
	{
		if (stream.isWriting)
		{
			stream.SendNext(rocketLeagueBall.ballRigidbody.transform.position);
			stream.SendNext(rocketLeagueBall.ballRigidbody.velocity);
			stream.SendNext(rocketLeagueBall.ballRigidbody.angularVelocity);
			stream.SendNext(PhotonNetwork.GetPing());
		}
		else
		{
			updateTime = Time.unscaledTime;
			dataUpdated = true;
			ballPosition = (Vector3)stream.ReceiveNext();
			ballVelocity = (Vector3)stream.ReceiveNext();
			ballAngularVelocity = (Vector3)stream.ReceiveNext();
			ping = (int)stream.ReceiveNext();
		}
	}

	private void FixedUpdate()
	{
		if (rocketLeagueBall.ballPhotonView.isMine || !(Time.unscaledTime - updateTime < 0.5f))
		{
			return;
		}
		float num = Vector3.Distance(base.transform.position, ballPosition);
		if (num < 30f)
		{
			if (num > 0.6f)
			{
				Vector3 b = ballPosition + ballVelocity * (Time.unscaledTime - updateTime + (float)((PhotonNetwork.GetPing() + ping) / 1000));
				base.transform.position = Vector3.Lerp(base.transform.position, b, Time.unscaledDeltaTime * 5f);
			}
		}
		else
		{
			base.transform.position = ballPosition;
			rocketLeagueBall.ballRigidbody.velocity = ballVelocity;
			rocketLeagueBall.ballRigidbody.angularVelocity = ballAngularVelocity;
		}
		if (dataUpdated)
		{
			rocketLeagueBall.ballRigidbody.velocity = ballVelocity;
			rocketLeagueBall.ballRigidbody.angularVelocity = ballAngularVelocity;
			dataUpdated = false;
		}
	}
}
